package com.forever.controller;
import com.forever.toolAndAgent.AiResult;
import com.forever.toolAndAgent.Assistant;
import com.forever.toolAndAgent.ChatAssistant;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.MemoryId;
import dev.langchain4j.service.Result;
import dev.langchain4j.service.UserMessage;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import dev.langchain4j.model.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping("/chat")
@RestController
public class ChatController {
    @Resource
    private ChatAssistant chatAssistant;

    @Resource
    private OpenAiChatModel openAiChatModel;

    ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);


    interface Assistant {
        String chat(@MemoryId int sessionId, @UserMessage String userMessage);
    }
    @GetMapping("/chatWithMemory")
    public String chatWithMemory(@RequestParam Integer sessionId, @RequestParam String message) {
        return chatAssistant.chat(sessionId,message);
    }

    @GetMapping("/chatWithMemory2")
    public String chatWithMemory2(@RequestParam Integer sessionId, @RequestParam String message) {
        ChatLanguageModel model = (ChatLanguageModel) openAiChatModel;
        Assistant assistant = AiServices.builder(Assistant.class)
                .chatLanguageModel(model)
                .chatMemoryProvider(memoryId -> chatMemory)
                .build();
        return assistant.chat(sessionId,message);
    }
}
